//
// Created by miaomiaole on 2023/5/18.
//

#ifndef CMAKECXX_CONTAINER_OBJ_H
#define CMAKECXX_CONTAINER_OBJ_H

#include <iostream>

using namespace std;

class Container_Obj {
public:
    Container_Obj() {
        cout << "generate Container_Obj" << endl;
    }

    virtual ~Container_Obj();
};

// FIXME 存在head无法读取
class Stack_Obj {
    struct Link {
        Container_Obj *data;
        Link *next;

        Link(Container_Obj *dat, Link *nex) : data(dat), next(nex) {
            cout << "generate Link " << endl;
        }
    } *head;

public:
    Stack_Obj() : head(0) {
        cout << "Stack_Obj" << endl;
    }

    ~Stack_Obj() {
        while (head) {
            delete pop();
        }
    }

    // 推数据
    void push(Container_Obj *data) {
        // 当前指针移动到下一个link元素
        this->head = new Link(data, this->head);
    }

    // 取顶栈数据
    Container_Obj *peek() const {
        return head ? head->data : 0;
    }

    //弹栈
    Container_Obj *pop() {
        if (head == 0) return 0;
        Container_Obj *res = head->data;
        Link *oldHead = head;
        head = head->next;
        delete oldHead;
        return res;
    }
};


#endif //CMAKECXX_CONTAINER_OBJ_H
